package com.yjhh.scala

import scala.xml.Node


class PersonSeq(val name: String,val age: Int) {

  def toMXL = {
    <person>
      <name>{name}</name>
      <age>{age}</age>
    </person>
  }

  def toPerson(xml : scala.xml.Elem): PersonSeq = {
    new PersonSeq((xml \ "name").text,(xml \ "age").text.toInt)
  }

  final override def toString: String = {
    "name="+name+", age="+age
  }

  def xmlMatching(node:Any)={
    node match {
      //XML模式匹配语法，利用{}进行匹配
      case <persons>{sub_element}</persons>=> println("111="+sub_element)
      case <persons>{sub_element @ _*}</persons>=> println("222="+sub_element)
      case ComputtationFramework2(name,popular) => println("222")
      //其它未匹配的情况
      case _ => println("no matching")
    }
  }

}

case class ComputtationFramework2(name: String, popular: Boolean) extends DataFrameWork


object XMLSerialization {

  def main(args: Array[String]): Unit = {
    val person = new PersonSeq("张三", 28)
    println((person.toMXL \ "name").text)

    val personSeq = person.toPerson(person.toMXL)
    println(personSeq.name+" "+personSeq.age)

    println(person)

    //保存到XML文件当中
    scala.xml.XML.save("d:/person.xml", person.toMXL, "UTF-8", true, null)

    val xmlElem = scala.xml.XML.loadFile("d:/person.xml")
    println(xmlElem \ "name")

    val seq = person.toPerson(xmlElem)
    println(seq)

    //下面这条语句的执行结果：<person><name>摇摆少年梦</name></person>
    person.xmlMatching(<persons><person><name>摇摆少年梦</name></person></persons>)
    person.xmlMatching(<persons><person><name>摇摆少年梦</name></person><person><name>摇摆少年梦</name></person></persons>)

    var com = ComputtationFramework2("aaa",true)
    person.xmlMatching(com)
  }

}
